package com.xyh.concurrency.example.concueerncyAqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class CountDownLatchDemo {

    private final static int cuont = 200;

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(cuont);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < cuont; i++) {
            int cotdowcount = i;
            executorService.execute(() -> {
                try {
                    test(cotdowcount);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();
                }


            });
        }
        //另一种写法就是设置制定的线程等待时间当多长时间没有反映之后就进行线程的丢弃操作
        countDownLatch.await(10, TimeUnit.MILLISECONDS);
        //当线程执行到最后一个后进行数据的转化
        countDownLatch.await();
        log.info("kaishi");
        executorService.shutdown();
    }

    public static void test(int cuont) throws InterruptedException {
        Thread.sleep(100);
        log.info("{}", cuont);

    }
}
